package org.algorithm.biubiu.wheel_2.链表;

import org.algorithm.biubiu.wheel_1.common.ListNode;

import java.util.List;

public class 反转部分链表 {

    public ListNode reverseBetween(ListNode head, int m, int n) {
        if(m>=n) return head;
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        head = dummy;

        for (int i = 1; i <m; i++) {
            head = head.next;
        }

        ListNode prev = head;
        ListNode leftNode = prev.next;

        ListNode left = leftNode;
        ListNode right = left.next;


        for(int i = m;i<n;i++){
            ListNode next = right.next;
            right.next = left;
            left = right;
            right = next;
        }
        prev.next = left;
        leftNode.next = right;
        return dummy.next;

    }
}
